﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using YArchitech.Controls;
using YArchitech.DataModel;
using YArchitech.LIB;
using YArchitech.LIB.YJKControls;
using Microsoft.Office.Interop.Word;

namespace HangerSupport
{
	public partial class DlgHSCalculation : System.Windows.Forms.Form
	{
		public DlgHSCalculation()
		{
			this.InitializeComponent();
			YJKProcessTools hyprocessTools = new YJKProcessTools();
			hyprocessTools.ShowInTaskbarChange(this);
			this.userSetting = new UserSetting();
			base.Icon = (base.Icon = Resource.YArchitechIcon);
			this.comboBox1.SelectedIndex = 0;
			this.comboBox2.SelectedIndex = 0;
			this.comboBox3.SelectedIndex = 0;
			this.comboBox4.SelectedIndex = 0;
			this.comboBox5.SelectedIndex = 0;
			this.comboBox6.SelectedIndex = 0;
			this.getDefault();
		}

		private void getDefault()
		{
			this.hyTextBox1.Text = ((this.userSetting.GetValue("HSCalculation", "R") == "") ? "0" : this.userSetting.GetValue("HSCalculation", "R"));
			this.hyTextBox2.Text = ((this.userSetting.GetValue("HSCalculation", "CableTrayW") == "") ? "0" : this.userSetting.GetValue("HSCalculation", "CableTrayW"));
			this.hyTextBox3.Text = ((this.userSetting.GetValue("HSCalculation", "DuctW") == "") ? "0" : this.userSetting.GetValue("HSCalculation", "DuctW"));
			this.hyTextBox4.Text = ((this.userSetting.GetValue("HSCalculation", "PipeW") == "") ? "0" : this.userSetting.GetValue("HSCalculation", "PipeW"));
		}

		private void setDefault()
		{
			this.userSetting.WriteValue("HSCalculation", "R", this.hyTextBox1.Text);
			this.userSetting.WriteValue("HSCalculation", "CableTrayW", this.hyTextBox2.Text);
			this.userSetting.WriteValue("HSCalculation", "DuctW", this.hyTextBox3.Text);
			this.userSetting.WriteValue("HSCalculation", "PipeW", this.hyTextBox4.Text);
		}

		private void button1_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void button3_Click(object sender, EventArgs e)
		{
			bool flag = !this.setCalValue();
			if (!flag)
			{
				this.label15.Text = string.Concat(new string[]
				{
					"=",
					this.γ,
					"*",
					this.η,
					"*",
					this.ζ1,
					"*",
					this.ζ2,
					"*",
					this.αmax
				});
				this.αEK = Convert.ToDouble(this.γ) * Convert.ToDouble(this.η) * Convert.ToDouble(this.ζ1) * Convert.ToDouble(this.ζ2) * Convert.ToDouble(this.αmax);
				bool flag2 = this.αEK < 0.5;
				if (flag2)
				{
					this.label16.Text = "=" + this.αEK.ToString() + "<0.5,取0.5";
				}
				else
				{
					this.label16.Text = "=" + this.αEK.ToString() + "≥0.5";
				}
				this.label18.Text = string.Concat(new string[]
				{
					"=",
					this.γEh,
					"*",
					((this.αEK < 0.5) ? 0.5 : this.αEK).ToString(),
					"*(",
					this.CableTrayW,
					"+",
					this.DuctW,
					"+",
					this.PipeW,
					")*9.8"
				});
				this.S = Convert.ToDouble(this.γEh) * ((this.αEK < 0.5) ? 0.5 : this.αEK) * (Convert.ToDouble(this.CableTrayW) + Convert.ToDouble(this.DuctW) + Convert.ToDouble(this.PipeW)) * 9.8;
				bool flag3 = this.S <= Convert.ToDouble(this.R);
				if (flag3)
				{
					this.label19.Text = string.Concat(new string[]
					{
						"=",
						this.S.ToString(),
						" N≤R=",
						this.R,
						" N,满足要求"
					});
				}
				else
				{
					this.label19.Text = string.Concat(new string[]
					{
						"=",
						this.S.ToString(),
						" N>R=",
						this.R,
						" N,不满足要求"
					});
				}
				this.showlabel();
				this.setDefault();
			}
		}

		private bool setCalValue()
		{
			List<System.Windows.Forms.Control> list = new List<System.Windows.Forms.Control>
			{
				this.comboBox1,
				this.comboBox2,
				this.comboBox3,
				this.comboBox4,
				this.comboBox5,
				this.comboBox6,
				this.hyTextBox1,
				this.hyTextBox2,
				this.hyTextBox3,
				this.hyTextBox4
			};
			foreach (System.Windows.Forms.Control control in list)
			{
				bool flag = !this.ValidControl(control);
				if (flag)
				{
					return false;
				}
			}
			this.γ = this.comboBox1.Text.ToString();
			this.η = this.comboBox2.Text.ToString();
			this.ζ1 = this.comboBox3.Text.ToString();
			this.ζ2 = this.comboBox4.Text.ToString();
			this.αmax = this.comboBox5.Text.ToString();
			this.γEh = this.comboBox6.Text.ToString();
			this.R = this.hyTextBox1.Text.ToString();
			this.CableTrayW = this.hyTextBox2.Text;
			this.DuctW = this.hyTextBox3.Text;
			this.PipeW = this.hyTextBox4.Text;
			return true;
		}

		private void showlabel()
		{
			this.label12.Show();
			this.label13.Show();
			this.label14.Show();
			this.label15.Show();
			this.label16.Show();
			this.label17.Show();
			this.label18.Show();
			this.label19.Show();
			this.toolTip.SetToolTip(this.label18, this.label18.Text);
		}

		private void button2_Click(object sender, EventArgs e)
		{
			bool flag = this.setCalValue();
			if (flag)
			{
				this.Run();
				this.setDefault();
			}
		}

		public void Run()
		{
			try
			{
				Microsoft.Office.Interop.Word.Application application = (Microsoft.Office.Interop.Word.Application)Activator.CreateInstance(Marshal.GetTypeFromCLSID(new Guid("000209FF-0000-0000-C000-000000000046")));
				object value = Missing.Value;
				object obj = Path.Combine(Product.DataLocation, "抗震支吊架计算书.docx");
				object value2 = Missing.Value;
				Document document = application.Documents.Open(ref obj, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2, ref value2);
				application.Visible = false;
				document.ActiveWindow.ActivePane.View.SeekView = WdSeekView.wdSeekMainDocument;
				object obj2 = WdReplace.wdReplaceAll;
				Dictionary<string, string> dictionary = new Dictionary<string, string>();
				bool flag = this.αEK < 0.5;
				if (flag)
				{
					dictionary.Add("{radio}", string.Concat(new string[]
					{
						this.γ,
						" * ",
						this.η,
						" * ",
						this.ζ1,
						" * ",
						this.ζ2,
						" * ",
						this.αmax,
						"=",
						this.αEK.ToString(),
						"＜0.5,取0.5"
					}));
				}
				else
				{
					dictionary.Add("{radio}", string.Concat(new string[]
					{
						this.γ,
						" * ",
						this.η,
						" * ",
						this.ζ1,
						" * ",
						this.ζ2,
						" * ",
						this.αmax,
						"=",
						this.αEK.ToString(),
						"≥0.5"
					}));
				}
				bool flag2 = this.S <= Convert.ToDouble(this.R);
				if (flag2)
				{
					dictionary.Add("{S}", string.Concat(new string[]
					{
						this.γEh,
						" * ",
						((this.αEK < 0.5) ? 0.5 : this.αEK).ToString(),
						"* (",
						this.CableTrayW,
						" + ",
						this.DuctW,
						" + ",
						this.PipeW,
						") * 9.8=",
						this.S.ToString(),
						" N ≤R=",
						this.R,
						" N,满足要求。"
					}));
				}
				else
				{
					dictionary.Add("{S}", string.Concat(new string[]
					{
						this.γEh,
						" * ",
						((this.αEK < 0.5) ? 0.5 : this.αEK).ToString(),
						"* (",
						this.CableTrayW,
						" + ",
						this.DuctW,
						" + ",
						this.PipeW,
						") * 9.8=",
						this.S.ToString(),
						" N >R=",
						this.R,
						" N,不满足要求。"
					}));
				}
				dictionary.Add("{GQK}", this.CableTrayW);
				dictionary.Add("{GFK}", this.DuctW);
				dictionary.Add("{GGK}", this.PipeW);
				dictionary.Add("{FunctionRadio}", this.γ);
				dictionary.Add("{TypeRadio}", this.η);
				dictionary.Add("{State1}", this.ζ1);
				dictionary.Add("{State2}", this.ζ2);
				dictionary.Add("{Max}", this.αmax);
				dictionary.Add("{EH}", this.γEh);
				dictionary.Add("{R}", this.R);
				foreach (KeyValuePair<string, string> item in dictionary)
				{
					this.replaceWord(application, ref value2, item, ref obj2);
				}
				int num = 0;
				string saveFilePath = FileTools.GetSaveFilePath("抗震支吊架计算书", ".docx", ref num);
				_Document document2 = document;
				object obj3 = saveFilePath;
				object missing = Type.Missing;
				object missing2 = Type.Missing;
				object missing3 = Type.Missing;
				object missing4 = Type.Missing;
				object missing5 = Type.Missing;
				object missing6 = Type.Missing;
				object missing7 = Type.Missing;
				object missing8 = Type.Missing;
				object missing9 = Type.Missing;
				object missing10 = Type.Missing;
				object missing11 = Type.Missing;
				object missing12 = Type.Missing;
				object missing13 = Type.Missing;
				object missing14 = Type.Missing;
				object missing15 = Type.Missing;
				document2.SaveAs(ref obj3, ref missing, ref missing2, ref missing3, ref missing4, ref missing5, ref missing6, ref missing7, ref missing8, ref missing9, ref missing10, ref missing11, ref missing12, ref missing13, ref missing14, ref missing15);
				document.Close(ref value2, ref value2, ref value2);
				application.Quit(ref value2, ref value2, ref value2);
				Process.Start(saveFilePath);
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				YJKMessageBox.Information("info");
				throw;
			}
		}

		private void replaceWord(Microsoft.Office.Interop.Word.Application wordApp, ref object oMissing, KeyValuePair<string, string> item, ref object replace)
		{
			wordApp.Selection.Find.Replacement.ClearFormatting();
			wordApp.Selection.Find.ClearFormatting();
			wordApp.Selection.Find.Text = item.Key;
			wordApp.Selection.Find.Replacement.Text = item.Value;
			wordApp.Selection.Find.Execute(ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref replace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
		}

		private void writeParagraph(Microsoft.Office.Interop.Word.Application wordApp, string content)
		{
			wordApp.Selection.Font.Bold = 0;
			wordApp.Selection.TypeText(content);
			wordApp.Selection.TypeParagraph();
		}

		private bool ValidControl(System.Windows.Forms.Control control)
		{
			bool flag = !this.isDouble(control.Text);
			bool result;
			if (flag)
			{
				YJKMessageBox.Information("请输入非负数字！");
				control.Focus();
				result = false;
			}
			else
			{
				result = true;
			}
			return result;
		}

		private bool isDouble(string val)
		{
			double num;
			bool flag = double.TryParse(val, out num);
			bool flag2 = flag;
			bool result;
			if (flag2)
			{
				bool flag3 = Convert.ToDouble(val) < 0.0;
				result = !flag3;
			}
			else
			{
				result = false;
			}
			return result;
		}

		private void comboBox1_Leave(object sender, EventArgs e)
		{
			this.JudgeParameter(this.label2, this.comboBox1);
		}

		private void comboBox2_Leave(object sender, EventArgs e)
		{
			this.JudgeParameter(this.label3, this.comboBox2);
		}

		private void comboBox3_Leave(object sender, EventArgs e)
		{
			this.JudgeParameter(this.label4, this.comboBox3);
		}

		private void comboBox4_Leave(object sender, EventArgs e)
		{
			this.JudgeParameter(this.label5, this.comboBox4);
		}

		private void comboBox5_Leave(object sender, EventArgs e)
		{
			this.JudgeParameter(this.label6, this.comboBox5);
		}

		private void comboBox6_Leave(object sender, EventArgs e)
		{
			this.JudgeParameter(this.label7, this.comboBox6);
		}

		private void JudgeParameter(Label lblName, System.Windows.Forms.ComboBox cbxName)
		{
			string text = lblName.Text + ":\n";
			bool flag = cbxName.Text.ToDouble(0.0) < 0.0 || cbxName.Text.ToDouble(0.0) > 2.0;
			if (flag)
			{
				text += "只能输入最小值为0，最大值为2.0的数！";
				YJKMessageBox.Show(text);
				cbxName.Focus();
				cbxName.SelectAll();
			}
			cbxName.Text = Math.Round(cbxName.Text.ToDouble(0.0), 2, MidpointRounding.AwayFromZero).ToString();
		}

		private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
		{
			this.IsNotLetter(e);
		}

		private void IsNotLetter(KeyPressEventArgs e)
		{
			bool flag = (e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != '\b' && e.KeyChar != '.';
			if (flag)
			{
				e.Handled = true;
			}
		}

		private string γ = "";

		private string η = "";

		private string ζ1 = "";

		private string ζ2 = "";

		private string αmax = "";

		private string γEh = "";

		private string R = "";

		private double αEK = 0.0;

		private string DuctW = "";

		private string PipeW = "";

		private string CableTrayW = "";

		private double S = 0.0;

		private UserSetting userSetting = null;
	}
}
